<template>
  <div>

  </div>
</template>

<script>
// import { getRoutesConfig } from '@/services/user'
import dispose from "../../api/dispose";
import { loadRoutes } from '@/utils/routerUtil'
import { setAuthorization } from '@/utils/request';
import { mapMutations } from 'vuex'
import httplogin from '../../api/login'

export default {
  name: 'callback',
  data() {
    return {
      provider: '',
      settingsService: '',
      queryParams: {
        code: '',
        state: ''
      }
    };
  },
  mounted() {
    // console.log(this.$route);
    console.log();
    this.ngOnInit()
  },
  methods: {

    ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),

    //获取当前url地址
    getUrlParams() {
      const url = window.location.href

      console.log(url);
      if (url.indexOf('/callback/') != -1) {

        let allUrl = url.split('?')[0]

        let parameter = url.split('?')[1]//所有参数
        let parameter1 = parameter.split('&')[0]
        let code = parameter1.split('=')[1]

        let parameter2 = parameter.split('&')[1]
        let state = parameter2.split('=')[1]

        this.queryParams = {
          code,
          state
        }


        let callbackGo = allUrl.split('/callback/')[1]

        this.provider = callbackGo

      }
    },

    async formatDataRoute() {
      let routerArr = [];
      let obj = {
        children: [],
        router: "root",
      };
      const res = await httplogin.getMenuTree();
      obj.children = res.data
      routerArr.push(obj);
      await loadRoutes(routerArr)

      this.$router.push('/home').catch(() => { })
      this.$message.success("登录成功", 3)
    },

    async ngOnInit() {

      this.getUrlParams()
      this.settingsService = this.$store.getters['account/user']
      if (!this.settingsService) {

        await dispose.callback(this.provider, this.queryParams).then((async res => {
          if (!res.data.code) {

            const { displayName, email, name, expired, refresh_token, token, ticket, username, type } = res.data.data

            let now = new Date();
            //设置时间
            now.setMinutes(now.getMinutes() + expired / 10)

            setAuthorization({ token: token, expireAt: now, online_ticket: ticket, congress: refresh_token })
            this.setUser(username)
            this.setPermissions(displayName)
            this.setRoles(type)

            this.formatDataRoute()

            // await this.$message.success("登录成功", 3)
            // await this.$router.push('/home').catch(() => { })

          } else {
            this.$router.push('/login').catch(() => { })
            this.$message.error("无绑定账号", 3)
          }
        }))
      } else {
        dispose.bindProvider(this.provider, this.queryParams).then((res => {
          if (!res.data.code) {
            this.$router.push('/components/palette').catch(() => { })
            this.$message.success("绑定成功", 3)
          } else {
            this.$router.push('/components/palette').catch(() => { })
            this.$message.error("绑定异常", 3)
          }
        }))
      }
    }
  }
}
</script>

<style scoped></style>
